﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Public/appsignal.h"
#include "Dialog/dialogversion.h"
#include "Control/Message/messagewidget.h"

#include <QKeyEvent>
#include <QFileDialog>
#include <QStandardPaths>

// test
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();

    setWindowTitle("Piscis");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    // 菜单信号绑定
    connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::slot_menu_open_triggered);
    connect(ui->actionClear, &QAction::triggered, this, &MainWindow::slot_menu_clear_triggered);
    connect(ui->actionExit, &QAction::triggered, this, [this]{ close(); });
    connect(ui->actionDeveloper, &QAction::triggered, this, []{ DialogVersion dialog; dialog.exec(); });

    // 系统信号
    connect(AppSignal::getInstance(), &AppSignal::sgl_system_output_message, this, &MainWindow::slot_system_output_message);

    ui->menuOpen->setWindowFlag(Qt::NoDropShadowWindowHint);
    ui->menuSetting->setWindowFlag(Qt::NoDropShadowWindowHint);
    ui->menuView->setWindowFlag(Qt::NoDropShadowWindowHint);
    ui->menuAbout->setWindowFlag(Qt::NoDropShadowWindowHint);

    showMaximized();
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::Modifier::CTRL)
    {

    }
    else if (event->key() == Qt::Key_Escape)
    {
        showNormal();
        ui->menubar->setVisible(true);
    }
    else if (event->key() == Qt::Key_A)
    {
        // 寓意 Add （左手操作也方便）
        ui->widgePtictureGalary->sendPreviousOne();
    }
    else if (event->key() == Qt::Key_D)
    {
        emit AppSignal::getInstance()->sgl_close_slected_graphics_item();
    }
    else if (event->key() == Qt::Key_H)
    {
        ui->widgetGraphicsView->switchSelectedItemVisiblity();
    }
    else if (event->key() == Qt::Key_Space)
    {
        if (!event->isAutoRepeat())
        {
            ui->widgetGraphicsView->switchInteractiveMode(WidgetGraphicsView::EnumDragMode::Mode_Drag);
        }
    }

    QMainWindow::keyPressEvent(event);
}

void MainWindow::keyReleaseEvent(QKeyEvent *ev)
{
    if (ev->key() == Qt::Key_Space)
    {
        if (!ev->isAutoRepeat())
        {
            qDebug() << "MainWindow::keyReleaseEvent Space";
            ui->widgetGraphicsView->switchInteractiveMode(WidgetGraphicsView::EnumDragMode::Mode_Select);
        }
    }
    QMainWindow::keyReleaseEvent(ev);
}

void MainWindow::slot_menu_open_triggered()
{
    QString desktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString path = QFileDialog::getExistingDirectory(this, tr("选择图像文件夹"), desktop, QFileDialog::ShowDirsOnly);
    QDir dir(path);
    if (path.isEmpty() || (!dir.exists())) return;

    ui->widgePtictureGalary->open(path);
}

void MainWindow::slot_menu_clear_triggered()
{
    ui->widgePtictureGalary->open("");
}

void MainWindow::slot_system_output_message(int type, const QString &message)
{
    MessageWidget *widget = new MessageWidget(type, MessageWidget::P_Top_Center, this);
    widget->showMessage(message);
}

